﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Navigation;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Example
{
    public partial class MainPage: PhoneApplicationPage
    {
        private Category _lion;

        private const string CategoryNameKey = "CategoryName";

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            if(Tree.SelectedCategory != null)
            {
                PhoneApplicationService.Current.State[CategoryNameKey] = Tree.SelectedCategory.Name;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if(PhoneApplicationService.Current.State.ContainsKey(CategoryNameKey))
            {
                _selectedName = PhoneApplicationService.Current.State[CategoryNameKey] as string;
            }

            base.OnNavigatedTo(e);
        }

        public MainPage()
        {
            InitializeComponent();

            Loaded += MainPage_Loaded;
            CreateTree();
            DataContext = this;
        }

        private void CreateTree()
        {
            _lion = new Category() { Name = "Lion" };

            animals = new Category()
            {
                Name = "Animals", SubCategories = new ObservableCollection<Category>()
                {
                    new Category()
                    {
                        Name = "Beast of preys", SubCategories = new ObservableCollection<Category>()
                        {
                            _lion,
                            new Category() { Name = "Tiger" },
                        }
                    },
                    new Category()
                    {
                        Name = "Herbivorous", SubCategories = new ObservableCollection<Category>()
                        {
                            new Category()
                            {
                                Name = "Rabbit",
                            },
                            new Category() { Name = "Cow" },
                        }
                    }
                }
            };

            Tree = new TreeModel()
            {
                Categories = new ObservableCollection<Category>()
                {
                    animals,
                    new Category()
                    {
                        Name = "Automobile ", SubCategories = new ObservableCollection<Category>()
                        {
                            new Category()
                            {
                                Name = "Trucks", SubCategories = new ObservableCollection<Category>()
                                {
                                    new Category()
                                    {
                                        Name = "Volvo", SubCategories = new ObservableCollection<Category>()
                                        {
                                            new Category()
                                            {
                                                Name = "Volvo FH", SubCategories = new ObservableCollection<Category>()
                                                {
                                                }
                                            },
                                        }
                                    },
                                }
                            },
                            new Category()
                            {
                                Name = "Car", SubCategories = new ObservableCollection<Category>()
                                {
                                    new Category() { Name = "Audi R8" },
                                }
                            },
                        }
                    },
                }
            };
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if(_selectedName != null)
            {
                Tree.SelectedCategory = Tree.FindCategoryByName(_selectedName);
            }
        }

        public TreeModel Tree { get; set; }

        private void ExpandTo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Tree.SelectedCategory = null;
            Tree.SelectedCategory = _lion;
        }

        private Category animals;
        private string _selectedName;
        private int _counter;

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            if(animals != null)
            {
                animals.SubCategories.Add(new Category() { Name = string.Format("Animal-{0}", _counter++) });
            }
        }
    }
}